<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="船名" prop="shipName">
        <el-input
          v-model.trim="queryParams.shipName"
          placeholder="请输入船名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="船舶编码" prop="shipCode">
        <el-input
          v-model.trim="queryParams.shipCode"
          placeholder="请输入船舶编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="船舶类型" prop="shipType">
        <el-select v-model="queryParams.shipType" placeholder="请选择船舶类型" clearable size="small">
          <el-option
            v-for="dict in shipTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="setPrint"
          v-hasPermi="['frontierEvent:event:export']"
        >打印
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shipList" id="tableList">
      <el-table-column label="船名" align="center" prop="shipName"/>
      <el-table-column label="船舶编码" align="center" prop="shipCode"/>
      <el-table-column label="船舶类型" align="center" prop="shipType" :formatter="shipTypeFormat"/>
      <el-table-column label="在船人员数量" align="center" prop="shipPersonNum"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div id="myChart"></div>

  </div>
</template>

<script>
  import {listShipPersonnelStatistics} from "@/api/basicInformation/ship";
  import echarts from "echarts";

  export default {
    name: "Ship",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 船舶信息表格数据
        shipList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 船舶类型字典
        shipTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          shipName: undefined,
          shipCode: undefined,
          shipType: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("ship_type").then(response => {
        this.shipTypeOptions = response.data;
      });
    },
    methods: {
      setPrint() {
        var html = document.getElementById("tableList").innerHTML;
        var w = window.open(location.href);
        var style = w.document.createElement("style");
        style.type = "text/css";
        style.innerText = `打印内容元素的样式...`;
        // w.document.getElementsByTagName("head").item(0).appendChild(style);
        var tableStr = "<table style='width:100%;'><tr><td style='text-align: center;'>船舶编码</td><td style='text-align: center;'>船舶类型</td><td style='text-align: center;'>在船人员数量</td></tr>";
        for (var i = 0; i < this.shipList.length; i++) {
          var shipTypeName = this.selectDictLabel(this.shipTypeOptions, this.shipList[i].shipType);
          var shipCode=this.shipList[i].shipCode === null ? "" : this.shipList[i].shipCode;
          tableStr += "<tr><td style='text-align: center;'>" + shipCode + "</td><td style='text-align: center;'>" + shipTypeName + "</td><td style='text-align: center;'>" + this.shipList[i].shipPersonNum + "</td></tr>";
        }
        tableStr += "</table>";
        w.document.body.innerHTML = tableStr;
        w.document.close();
        w.print();
        w.close();
      },
      /** 查询船舶信息列表 */
      getList() {
        this.loading = true;
        listShipPersonnelStatistics(this.queryParams).then(response => {
          this.shipList = response.rows;
          this.total = response.total;
          this.loading = false;

          let nameList = [];
          let dataList = [];
          for (var i = 0; i < response.rows.length; i++) {
            nameList.push(response.rows[i].shipCode);
            dataList.push(response.rows[i].shipPersonNum);
          }

          // 实例化echarts对象
          let myChartDrawer = echarts.init(document.getElementById('myChart'))

          // 绘制条形图
          var option = {
            color: ['#91c7ae'],
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'line'
              }
            },
            title: {
              text: '在船人数统计',
              top: 5,
              left: 'center'
            },
            legend: {
              data: ['人数'],
              top: 30
            },
            // X轴
            xAxis: {
              data: nameList
            },
            // Y轴
            yAxis: {},
            // 数据
            series: [
              {
                name: '人数',
                type: 'bar',
                data: dataList,
                barWidth: 30
              }
            ]
          };

          myChartDrawer.setOption(option);
        });
      },
      // 船舶类型字典翻译
      shipTypeFormat(row, column) {
        return this.selectDictLabel(this.shipTypeOptions, row.shipType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery();
      }
    }
  };
</script>

<style>
  #myChart {
    width: 100%;
    min-height: 400px;
    clear: both;
    box-sizing: border-box;
    margin: 80px auto;
  }

  .cellNow {
    text-align: left;
  }
</style>
